package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.behavior.BehaviorAddDto;
import com.teacher.pojo.dto.behavior.BehaviorGetDto;
import com.teacher.pojo.dto.behavior.BehaviorUpdateDto;
import com.teacher.pojo.entity.Behavior;
import com.teacher.pojo.vo.behavior.BehaviorGetVo;
import com.teacher.service.IBehaviorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 日常行为规范-扣分	 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/pc/behavior")
@Api(tags = "日常行为规范-扣分")
public class BehaviorController {

    @Autowired
    private IBehaviorService iBehaviorService;

    /**
     * 添加日常行为规范
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加日常行为规范 behavior_add")
    @PostMapping
    @PreAuthorize("hasAuthority('behavior_add')")
    public CommonResult<String> add(@RequestBody @Validated BehaviorAddDto dto) {
        return iBehaviorService.add(dto);
    }

    /**
     * 更新日常行为规范
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新日常行为规范 behavior_update")
    @PutMapping
    @PreAuthorize("hasAuthority('behavior_update')")
    public CommonResult<String> update(@RequestBody @Validated BehaviorUpdateDto dto) {
        Behavior behavior = new Behavior();
        BeanUtils.copyProperties(dto, behavior);
        if (iBehaviorService.updateById(behavior)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除日常行为规范
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除日常行为规范 behavior_delete")
    @PreAuthorize("hasAuthority('behavior_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iBehaviorService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询日常行为规范
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询日常行为规范 behavior_get")
    @PreAuthorize("hasAuthority('behavior_get')")
    public CommonResult<IPage<BehaviorGetVo>> getPage(BehaviorGetDto dto) {
        return iBehaviorService.getPage(dto);
    }

    /**
     * 导入日常行为规范
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入日常行为规范 behavior_upload")
    @PreAuthorize("hasAuthority('behavior_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iBehaviorService.upload(file);
    }

}

